package com.zjut.an.util.system;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import com.zjut.an.util.tools.MD5;


public class SMSHelper {

	@Resource(name = "MD5")
	private static MD5 md5;
	/**
	 * 返回正确格式的号码
	 * @param mobiles
	 */
	public static String generateMobiles(String mobiles){
		String[] mobileArray = mobiles.split(";");
		StringBuffer sb = new StringBuffer();
		for (String mobile : mobileArray) {
			if (mobile.matches("1[3-9][0-9]{9}")) {
				sb.append(mobile).append(",");
			}
		}
		return sb.toString();
	}
	
	public static String Date2String(Date d) { 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		return formatter.format(d);
	}
	/**亿美联通
	 * @param args
	 * @throws IOException
	 */
	public static String sendSMS(String mobiles, String content, String kzm) throws IOException {
		if (mobiles=="" || content=="") {
			return "null param";
		}
		StringBuffer sb = new StringBuffer("http://www.btom.cn:8080/simpleinter/sendSMS?");
		String appId="EUCP-EMY-SMS1-0PQVJ";
		String timestamp = toString(new Date(), "yyyyMMddHHmmss");
		String secretKey = "5042233E29FEFCF9";
		String sign = md5.toMD5(appId+secretKey+timestamp);	

		sb.append("appId="+appId);
		sb.append("&timestamp="+timestamp);
		//sb.append("&kzm="+kzm);
		sb.append("&sign="+sign);
		sb.append("&mobiles="+mobiles);
		sb.append("&content=" + URLEncoder.encode(content, "UTF-8"));
		sb.append("&extendedCode="+kzm);
		// 创建url对象
		URL url = new URL(sb.toString());
		// 打开url连接
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		// 设置url请求方式 post
		connection.setRequestMethod("POST");
		// 发送
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		// 返回发送结果
		String inputline = in.readLine();		
		System.out.println("#####send sms:"+inputline);
		// 输出结果
		return inputline;
	}

	public static String toString(Date date, String format) {
		String dateStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			dateStr = sdf.format(date);
		} catch (Exception e) {
		}
		return dateStr;
	}
	/**
	 * @param args
	 * @throws IOException
	 */
	/*
	 * public static String sendSMS(String mobiles, String content) throws
	 * IOException {
	 * 
	 * if (mobiles=="" || content=="") { return "null param"; }
	 * 
	 * // 创建StringBuffer对象用来操作字符串 StringBuffer sb = new
	 * StringBuffer("http://m.5c.com.cn/api/send/?");
	 * 
	 * // APIKEY sb.append("apikey=866c2f24010b5712f695da084622c9bc");
	 * 
	 * // 用户名 sb.append("&username=zjutaa");
	 * 
	 * // 向StringBuffer追加密码 sb.append("&password=Mlrt1234");
	 * 
	 * // 向StringBuffer追加手机号码 sb.append("&mobile="+mobiles);
	 * 
	 * // 向StringBuffer追加消息内容转URL标准码 sb.append("&content=" +
	 * URLEncoder.encode(content, "GBK"));
	 * 
	 * // 创建url对象 URL url = new URL(sb.toString());
	 * 
	 * // 打开url连接 HttpURLConnection connection = (HttpURLConnection)
	 * url.openConnection();
	 * 
	 * // 设置url请求方式 ‘get’ 或者 ‘post’ connection.setRequestMethod("POST");
	 * 
	 * // 发送 BufferedReader in = new BufferedReader(new InputStreamReader(
	 * url.openStream()));
	 * 
	 * // 返回发送结果 String inputline = in.readLine();
	 * 
	 * System.out.println("#####send sms:"+inputline); // 输出结果 return inputline;
	 * 
	 * }
	 */
}
